

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript" src="helpman_topicinit.js"></script>

<title>Code Templates [Enterprise Architect User Guide]</title>

<meta name="keywords" content="Enterprise Architect, Sparx Systems, UML, Code Template,Code Template Overview">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />

<link type="text/css" href="default.css" rel="stylesheet" />
<style type="text/css">
  A, A:Visited
  {
    color: #00F;
  }

  A:Active, A:Hover
  {
    color: #F00;
  }

  OL
  {
    margin: 5px 0 5px 48px;
  }

  UL
  {
    margin-bottom: 3px;
    margin-top: 0;
  }

  #nsr
  {
    background-color: #D4DFFF;
  }

  #nsr A
  {
    text-decoration: none;
  }

  #mainbody
  {
    padding: 8px;
  }

  .p_BodyText
  {
    padding: 5px 0;
  }
</style>



<style type="text/css" media="screen">
  BODY
  {
    background-color: #FFFFFF;
  }

  #nsr
  {
    padding: 6px 6px 0 6px;
    border-bottom: none;
    vertical-align: top;
    z-index: 2;
    visibility: visible;
    left: 0;
    top: 0;
    position: absolute;
  }

  #mainbody
  {
    left: 0;
    top: 0;
    margin: 0;
    position: absolute;
    padding: 10px;
    overflow: auto;
    height: 100%;
    z-index: 1;
    background-repeat: no-repeat;
    background-position: bottom right;
    background-attachment: fixed;
  }
</style>

<style type="text/css" media="print">
  #nsr
  {
    visibility: none;
  }

  #mainbody
  {
    overflow: visible;
  }
</style>
<script type="text/javascript" language="JavaScript" src="nonscroll.js"></script>

</head>
<body scroll="no">

<div id="page">



        <div id="content">
          <div id="content-inner">
          



  
          <div id="nsr">
            <table border="0" cellspacing="0" cellpadding="4" width="100%">
              <tr valign="top">
                <td align="left">
  


                  <p class="p_Heading1"><span class="f_Heading1">Code Templates</span></p>



  
                </td>
                <td align="right">
                  <a href="introduction.htm" onmouseover="document.images.main.src='button_main_h.gif'" onmouseout="document.images.main.src='button_main.gif'">
                    <img name="main" src="button_main.gif" border=0 alt="Return to Introduction">
                  </a>&nbsp;
    
                  <a href="codetemplates.htm" onmouseover="document.images.prev.src='button_prev_h.gif'" onmouseout="document.images.prev.src='button_prev.gif'">
                    <img name=prev src="button_prev.gif" border=0 alt="Previous page" />
                  </a>&nbsp;
    
    
    
                  <a href="basetemplates.htm" onmouseover="document.images.next.src='button_next_h.gif'" onmouseout="document.images.next.src='button_next.gif'">
                    <img name=next src="button_next.gif" border=0 alt="Next page" />
                  </a>
    
    
                </td>
              </tr>
            </table>
          </div>
          <div id="mainbody">
  


                <p class="p_BodyText"><span class="f_BodyText">Code templates enable you to customize code generation of existing languages. For example:</span></p>
<ul style="text-indent: 0px; margin-left: 30px; list-style-position: outside;">
<li><span class="f_Bulletlist1">Modify the file headers created when generating new files</span></li>
<li><span class="f_Bulletlist1">Change the style of the generated code (such as indenting or brace position) to match the required coding standards</span></li>
<li><span class="f_Bulletlist1">Handle particular stereotypes to generate things like specialized method bodies and extra methods.</span></li>
</ul>
<p class="p_BodyText"><span class="f_BodyText">They also enable you to add code generation of entirely new languages that Enterprise Architect would otherwise not be able to handle. In this situation it is most useful to combine code templates with an <a href="addingcodemodulesinmdgtec.htm">MDG technology file</a> that includes the datatypes, and options for default file extensions.</span></p>
<p class="p_BodyText"><span class="f_BodyText">Enterprise Architect's <a href="basetemplates.htm">base code templates</a> specify the transformation from UML elements to the various parts of a given programming language. The templates are written as plain text with a syntax that shares some aspects of both mark-up languages and scripting languages. A simple example of a template used by Enterprise Architect is the 'Class template'. It is used to generate source code from a UML Class:</span></p>
<p class="p_Code"><span class="f_Code">%ClassNotes%</span></p>
<p class="p_Code"><span class="f_Code">%ClassDeclaration%</span></p>
<p class="p_Code"><span class="f_Code">%ClassBody%</span></p>
<p class="p_BodyText"><span class="f_BodyText">The above template simply refers to three other templates, namely </span><span class="f_BodyText" style="font-style: italic;">ClassNotes</span><span class="f_BodyText">, </span><span class="f_BodyText" style="font-style: italic;">ClassDeclaration</span><span class="f_BodyText"> and </span><span class="f_BodyText" style="font-style: italic;">ClassBody</span><span class="f_BodyText">. The enclosing percent (%) signs indicate a </span><span class="f_BodyText" style="font-style: italic;">macro</span><span class="f_BodyText">. Code Templates consist of various types of macros, each resulting in a substitution in the generated output. For a language such as C++, the result of processing the above template might be:</span></p>
<p class="p_Code"><span class="f_Code">/**</span></p>
<p class="p_Code"><span class="f_Code"> * This is an example class note generated using code templates</span></p>
<p class="p_Code"><span class="f_Code"> * @author Sparx Systems</span></p>
<p class="p_Code"><span class="f_Code"> */</span></p>
<p class="p_Code"><span class="f_Code">class ClassA: public ClassB</span></p>
<p class="p_Code"><span class="f_Code">{</span></p>
<p class="p_Code"><span class="f_Code">...</span></p>
<p class="p_Code"><span class="f_Code">}</span></p>
<p class="p_SubHeadingL1"><span class="f_SubHeadingL1">Execution of Code Templates</span></p>
<p class="p_BodyText"><span class="f_BodyText">A reference to a template (such as the </span><span class="f_BodyText" style="font-style: italic;">%ClassNotes%</span><span class="f_BodyText"> macro, from our example above) results in the execution of that template.</span></p>
<p class="p_BodyText"><span class="f_BodyText">Each template is designed for use with a particular element. For example the </span><span class="f_BodyText" style="font-style: italic;">ClassNotes</span><span class="f_BodyText"> template is to be used with UML Class elements.</span></p>
<p class="p_BodyText"><span class="f_BodyText">The element that is currently being generated is said to be </span><span class="f_BodyText" style="font-style: italic;">in scope</span><span class="f_BodyText">. If the element in scope is stereotyped Enterprise Architect looks for a template that has been defined for that stereotype. If a match is found, the specialized template is executed. Otherwise the default implementation of the base template is used.</span></p>
<p class="p_BodyText"><span class="f_BodyText">Templates are processed sequentially, line by line, replacing each macro with its underlying text value from the model.</span></p>




            </div>
          </div>
        </div>



</div>


</body>
</html>
